package com.linjiahao.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.eduservice.entity.EduVideo;
import com.linjiahao.eduservice.service.EduVideoService;
import com.linjiahao.eduservice.vo.VideoInfoForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-24
 */
@Api(description="课时管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @ApiOperation(value = "新增课时")
    @PostMapping("save-video-info")
    public ResultMap save(@ApiParam(name = "videoForm", value = "课时对象", required = true)
            @RequestBody VideoInfoForm videoInfoForm){
        System.out.println(videoInfoForm.toString());
        videoService.saveVideoInfo(videoInfoForm);
        return ResultMap.ok();
    }

    @ApiOperation(value = "根据ID查询课时")
    @GetMapping("video-info/{id}")
    public ResultMap getVideInfoById(
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id){

        VideoInfoForm videoInfoForm = videoService.getVideoInfoFormById(id);
        return ResultMap.ok().data("item", videoInfoForm);
    }

    @ApiOperation(value = "更新课时")
    @PutMapping("update-video-info/{id}")
    public ResultMap updateCourseInfoById(
            @ApiParam(name = "VideoInfoForm", value = "课时基本信息", required = true)
            @RequestBody VideoInfoForm videoInfoForm,
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id){
        System.out.println(videoInfoForm.toString());
        videoService.updateVideoInfoById(videoInfoForm);
        return ResultMap.ok();
    }

    @ApiOperation(value = "根据ID删除课时")
    @DeleteMapping("{id}")
    public ResultMap removeById(
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id){

        boolean result = videoService.removeVideoById(id);
        if(result){
            return ResultMap.ok();
        }else{
            return ResultMap.error().message("删除失败,服务开小差");
        }
    }

    @ApiOperation(value = "查询视频是否免费")
    @GetMapping("{id}")
    public ResultMap getIsFree(
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id){
        EduVideo byId = videoService.getOne(new QueryWrapper<EduVideo>().eq("video_source_id",id));
        return ResultMap.ok().data("isFree",byId.getIsFree()).data("courseId",byId.getCourseId());
    }
}

